import { getUserProfileApi, getUserApi, uploadPhotoApi, updateUserProfileApi } from '@/api/user';
import { AppDispatch } from '@/store/index';
import { userType } from '../reducer/user';

export const getUserProfileAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await getUserProfileApi();
    dispatch({ type: 'user/saveUserInfo', payload: res.data });
  };
};
export const getUserAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await getUserApi();
    dispatch({ type: 'user/saveUser', payload: res.data });
  };
};

export const uploadPhotoAction = (formData: FormData) => {
  return async (dispatch: AppDispatch) => {
    const res = await uploadPhotoApi(formData);
    // 第二种写法，直接调用getUserAction重新获取数据
    // dispatch({ type: 'user/updatePhoto', payload: res.data.photo });
  };
};

export const updateUserProfileAction = (data: Partial<userType>) => {
  return async () => {
    await updateUserProfileApi(data);
  };
};
